package com.atguigu.gulimail.product.controller;

import java.util.Arrays;
import java.util.Map;


import com.atguigu.common.valid.AddGroup;
import com.atguigu.common.valid.UpdateGroup;
import com.atguigu.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.atguigu.gulimail.product.entity.BrandEntity;
import com.atguigu.gulimail.product.service.BrandService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Response;


/**
 * 品牌
 *
 * @author fk
 * @email hahahahha@gmail.com
 * @date 2020-08-09 23:15:28
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:brand:list")
    public Response list (@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);
        return Response.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    // @RequiresPermissions("product:brand:info")
    public Response info (@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);
        return Response.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //  @RequiresPermissions("product:brand:save")
    public Response save (@RequestBody @Validated(AddGroup.class) BrandEntity brand /*BindingResult result*/) {
//        if(result.hasErrors()){
//            Map<String,String> map = new HashMap<>();
//            //1.获取错误的校验结果
//            result.getFieldErrors().forEach((item)->{
//                //获取发生错误时的message
//                String message = item.getDefaultMessage();
//                //获取发生错误的字段
//                String field = item.getField();
//                map.put(field,message);
//            });
//            return R.error(400,"提交的数据不合法").put("data",map);
//        }else {
//
//        }
        brandService.save(brand);
        return Response.ok();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:brand:update")
    public Response update (@RequestBody @Validated(UpdateGroup.class) BrandEntity brand) {
        brandService.updateDetail(brand);
        return Response.ok();
    }

    /**
     * 修改状态
     */
    @RequestMapping("/update/status")
    // @RequiresPermissions("product:brand:update")
    public Response updateStatus (@RequestBody @Validated(UpdateStatusGroup.class) BrandEntity brand) {
        brandService.updateById(brand);
        return Response.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:brand:delete")
    public Response delete (@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));
        return Response.ok();
    }

}
